import sys
sys.path.append("../")

from model.CommonDb import *

class ClassController:
    def __init__(self,args):
        self.args = args
        self.table = args["model"]

    def run(self):
        if self.args["func"] == "getData":
            return self.getData()
        elif self.args["func"] == "add":
            return self.add(self.args["data"])
        elif self.args["func"] == "getDataByParams":
            return self.getDataByParams(self.args["data"])
        elif self.args["func"] == "update":
            return self.update(self.args["data"])
        elif self.args["func"] == "delete":
            return self.delete(self.args["data"])

    def getDataByParams(self,data):
        db = CommonDb(self.table)
        wherestr = ""
        for key in data.keys():
            wherestr += " and " + key + "=" + "'" + data[key] + "' "
        rs = db.selectRowDataByWhere("*",wherestr)
        return rs

    def getData(self):
        db = CommonDb(self.table)
        rs = db.executeMap("select * from "+self.table+";")
        return rs

    def add(self,data):
        db = CommonDb(self.table)
        rs = db.add(data)
        return rs

    def update(self,data):
        db = CommonDb(self.table)
        return db.update(data," id="+str(data["id"]))

    def delete(self,data):
        db = CommonDb(self.table)
        return db.delete_where(" id="+str(data["id"]))